package main

import (
	"fmt"
	"io"
	"math/rand"
	"os"
	"strings"
	"time"

	"gitee.com/libs_2/assist/xinlian"
	"gitee.com/libs_2/assist/yunpian"
)

func main() {
	fmt.Println("assistant running....")

	meituiYunpian()
}

func meituiYunpian() {
	client := yunpian.NewClient("")
	f, err := os.Open("./mobile.txt")
	if err != nil {
		panic(err)
	}
	data, err := io.ReadAll(f)
	if err != nil {
		panic(err)
	}

	arr := strings.Split(string(data), ",")
	rand.Seed(time.Now().Unix())
	for idx, mobile := range arr {
		if mobile == "" || strings.Contains(mobile, "-") || strings.Contains(mobile, "_") {
			continue
		}

		s := rand.Int31n(100)%2 + 1
		fmt.Printf("\t%d 正在向%s发送,将休眠%d秒\n", idx, mobile, s)
		/*
		   -- 查询最新的可用女账号300个
		   SELECT ur.phone FROM user_info ui INNER JOIN user_register ur on ui.user_id=ur.id
		   WHERE ui.gender=2 AND ui.status=2 ORDER BY ui.user_id DESC LIMIT 300;

		   -- 查询最新的可用非女账号
		   SELECT ui.user_id,ur.phone FROM user_info ui INNER JOIN user_register ur on ui.user_id=ur.id
		   WHERE ui.gender!=2 AND ui.status=2 ORDER BY ui.user_id;
		*/

		// 女情况1
		// err := client.SendSingle(mobile, "【火柴俱乐部】您已成功报名“美腿大赛”，10元红包已到账！请更新到最新版本领取。")

		// 女情况2
		// err := client.SendSingle(mobile, "【火柴俱乐部】您的“美腿大赛”报名通道已开启！注：请更新到最新版本使用。")

		// 非女手机号
		err := client.SendSingle(mobile, "【xx】您的“美腿大赛”投票通道已开启！注：请更新到最新版本使用。")
		if err != nil {
			// panic(fmt.Errorf("phone:%s err:%w \n ", mobile, err))
			fmt.Printf("phone:%s err:%v \n ", mobile, err)
		}
		time.Sleep(time.Duration(s) * time.Second)
	}
}

func code1() {
	account := "800017_GZLT"
	pwd := "cnHDaYTrQb"
	client := xinlian.NewClient(account, pwd)
	f, err := os.Open("./mobile.txt")
	if err != nil {
		panic(err)
	}
	data, err := io.ReadAll(f)
	if err != nil {
		panic(err)
	}

	arr := strings.Split(string(data), ",")
	rand.Seed(time.Now().Unix())
	for _, mobile := range arr {
		if mobile == "" || strings.Contains(mobile, "-") || strings.Contains(mobile, "_") {
			continue
		}

		s := rand.Int31n(100)%5 + 1
		fmt.Printf("\t正在向%s发送,将休眠%d秒\n", mobile, s)
		// code := RandNumber(4)
		// fmt.Printf("\t正在向%s发送，验证码%s,将休眠%d秒\n", mobile, code, s)
		// err := client.SendSingle(mobile, fmt.Sprintf("【火柴俱乐部】您开通私聊功能的验证码是%s，10分钟内有效。", code))
		err := client.SendSingle(mobile, "【xx】您的私聊功能已开通，请打开APP使用~")
		if err != nil {
			panic(fmt.Errorf("phone:%s err:%w \n ", mobile, err))
		}
		time.Sleep(time.Duration(s) * time.Second)
	}
}

func RandNumber(len int) string {
	return string(RandStringK(len, RandKindNum, false))
}

const (
	RandKindNum   = 1 << 0 // 纯数字
	RandKindLower = 1 << 1 // 小写字母
	RandKindUpper = 1 << 2 // 大写字母
	RandKindAll   = RandKindNum | RandKindLower | RandKindUpper
)

var randSource = rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
var RandSelectSliceMap = map[int][]int{
	1: {1, 2, 3, 4, 5, 6, 7, 8, 9},                                                                //排除了0
	2: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, //排除了o
	3: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, //排除了O
}

// 随机字符串
func RandStringK(size int, kind int, isExclude0oO bool) []byte {
	scope := make([][]int, 0, 3)
	//if kind&RandKindNum != 0 {
	//	scope = append(scope, []int{10, 48})
	//}
	//if kind&RandKindLower != 0 {
	//	scope = append(scope, []int{26, 97})
	//}
	//if kind&RandKindUpper != 0 {
	//	scope = append(scope, []int{26, 65})
	//}

	if kind&RandKindNum != 0 {
		scope = append(scope, []int{9, 48, 1})
	}
	if kind&RandKindLower != 0 {
		scope = append(scope, []int{25, 97, 2})
	}
	if kind&RandKindUpper != 0 {
		scope = append(scope, []int{25, 65, 3})
	}

	result := make([]byte, size)
	l := len(scope)
	for i := 0; i < size; i++ {
		index := randSource.Intn(l)
		s, base, mapKey := scope[index][0], scope[index][1], scope[index][2]
		if isExclude0oO {
			result[i] = uint8(base + RandSelectSliceMap[mapKey][randSource.Intn(s)])
		} else {
			result[i] = uint8(base + randSource.Intn(s))
		}
	}
	return result
}
